import org.jetbrains.annotations.NotNull;

public class TestPC {
    public static void main(String[] args) {
        Tv tv = new Tv();
        new Player(tv).start();
        new Watcher(tv).start();
    }
}

class Player extends Thread {
    private Tv tv;

    public Player(@NotNull Tv tv) {
        super("player:");
        this.tv = tv;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            if ((i & 1) == 0) {
                tv.play("小熊维尼");
            } else {
                tv.play("广告");
            }
        }
    }
}

class Watcher extends Thread {
    private Tv tv;

    public Watcher(@NotNull Tv tv) {
        super("watcher");
        this.tv = tv;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            tv.watch();
        }
    }
}

class Tv {
    /**
     * 是否在表演
     */
    private boolean flag;
    private String program;

    public synchronized void play(String program) {
        while (flag) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("演员表演了" + program);
        flag = true;
        this.program = program;
        notifyAll();
    }

    public synchronized void watch() {
        while (!flag) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("观众观看了" + program);
        flag = false;
        notifyAll();
    }
}